package com.shopping.common.core.util;

import cn.hutool.core.collection.CollectionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

import java.util.*;

@Slf4j
public class MyModelUtil {

    public static <S, T> List<T> copyCollectionTo(List<S> sourceList, Class<T> targetClass) {
        if (CollectionUtil.isEmpty(sourceList)) {
            return null;
        }
        List<T> targetList = new LinkedList<>();
        for (S source : sourceList) {
            try {
                T target = targetClass.newInstance();
                BeanUtils.copyProperties(source, target);
                targetList.add(target);
            } catch (Exception e) {
                log.error("Failed to call MyModelUtil.copyCollectionTo", e);
                return Collections.emptyList();
            }
        }
        return targetList;
    }
}

